package httpRequest;

import com.alibaba.fastjson.JSON;
import httpRequest.BigDataVO.BDResponseDataVO;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Test;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 测试大数据接口
 * @author ： cww
 * @date : 2018/7/18
 */
public class bigDataRequest {

    @Test
    public void request(){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestBuilder requestBuilder = RequestBuilder.get();
        requestBuilder.setUri("http://172.16.123.82:8880/spider-web/api/externalService/getData.json");
        List list = new ArrayList<Map<String,String>>();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("dataType", "entArchives");
        // entArchives  dataType
        Map<String,String> paramMap = new HashMap<String, String>();
        paramMap.put("name", "福建闽泰科技发展有限公司");
        map.put("params", paramMap);
        list.add(map);
        String jsonString = JSON.toJSONString(list);
        System.out.println(jsonString);
        Map<String,String> checkMap = new HashMap<String, String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String ts =sdf.format(new Date());
        checkMap.put("ts", ts);
        checkMap.put("cs", "entArchives");
        // entArchives 请求来源
        checkMap.put("chs", MD5Utils.MD5(ts+"entArchives"+"F999AA0E80B9465F83B79D56982E678D"));
        requestBuilder.addParameter("params", jsonString);
        System.out.println(JSON.toJSONString(checkMap));
        requestBuilder.addParameter("checkJSON", JSON.toJSONString(checkMap));
        HttpUriRequest request = requestBuilder.build();
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(request);
            String result = IOUtils.toString(response.getEntity().getContent());
            BDResponseDataVO bdResponseDataVO = JSON.parseObject(result,BDResponseDataVO.class);
            System.out.println(bdResponseDataVO.getResult().get(0).getS_ENTERPRISE_INFO().getBaseInfo().getName());
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("end");

    }
}
